package com.company.cloud.user.modules.bus.producer;

import com.alibaba.fastjson.JSON;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.user.modules.app.controller.req.UserLoginBaseParam;
import com.company.common.server.config.cloud.ProjectConfig;
import com.company.common.server.config.mq.constant.MQTopicKeys;
import com.company.common.server.config.mq.models.MQUserLoginModel;
import com.company.common.server.config.mq.models.MQUserLogoutModel;
import com.company.common.server.config.mq.models.MQUserRegisterModel;
import com.company.common.server.config.mq.utils.SaasMqUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserProducer {


    @Autowired
    private ProjectConfig projectConfig;


    /**
     * 用户注册
     */
    public void userRegister(String registerType, UserLoginBaseParam param, String disFlg, LoginUserInfo user) {
        MQUserRegisterModel data = new MQUserRegisterModel();
        data.setCompanyNo(user.getCompanyNo());
        data.setUserId(user.getId());
        data.setThirdUserId(user.getThirdUserId());
        data.setThirdOpenId(user.getThirdOpenId());
        data.setUserName(user.getUserName());
        data.setToken(user.getToken());
        data.setRegisterType(registerType);
        data.setIp(user.getIp());
        data.setDisFlg(disFlg);
        data.setClientType(param.getClientType());
        data.setMobileNo(param.getClientDeviceNo());
        data.setLlType(param.getLlType());
        data.setLongitude(param.getLongitude());
        data.setLatitude(param.getLatitude());
        data.setOpDate(LocalDateTime.now());
        SaasMqUtils.produce(MQTopicKeys.MQ_UserRegister_Topic, JSON.toJSONString(data), true, projectConfig);
    }


    /**
     * 用户登录
     */
    public void userLogin(String loginType, UserLoginBaseParam param, LoginUserInfo user) {
        MQUserLoginModel data = new MQUserLoginModel();
        data.setCompanyNo(user.getCompanyNo());
        data.setUserId(user.getId());
        data.setThirdUserId(user.getThirdUserId());
        data.setUserName(user.getUserName());
        data.setLoginType(loginType);
        data.setToken(user.getToken());
        data.setIp(user.getIp());
        data.setClientType(param.getClientType());
        data.setMobileNo(param.getClientDeviceNo());
        data.setLlType(param.getLlType());
        data.setLongitude(param.getLongitude());
        data.setLatitude(param.getLatitude());
        data.setOpDate(LocalDateTime.now());
        SaasMqUtils.produce(MQTopicKeys.MQ_UserLogin_Topic, JSON.toJSONString(data), true, projectConfig);
    }


    /**
     * 用户注销
     */
    public void userLogout(Integer userId, String token, String ip) {
        MQUserLogoutModel data = new MQUserLogoutModel();
        data.setUserId(userId);
        data.setToken(token);
        data.setIp(ip);
        data.setOpDate(LocalDateTime.now());
        SaasMqUtils.produce(MQTopicKeys.MQ_UserLogout_Topic, JSON.toJSONString(data), true, projectConfig);
    }


}
